package com.cskaoyan.service;

import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月31日 21:23
 */
public class AdminTopicServiceimpl implements AdminTopicService {
    @Override
    public CommonData<MarketTopic> querrySelective(String title, String subtitle, Integer page, Integer limit, String sort, String order) {
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(title)) {
            criteria.andTitleLike(title);
        }
        if (!StringUtil.isEmpty(subtitle)) {
            criteria.andSubtitleLike(subtitle);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        List<MarketTopic> marketTopics = mapper.selectByExampleWithBLOBs(example);

        return CommonData.data(marketTopics);

    }

    @Override
    public void add(MarketTopic marketTopic) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        mapper.insertSelective(marketTopic);
    }

    @Override
    public MarketTopic selectOne(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        MarketTopic marketTopic = mapper.selectByPrimaryKey(id);
        return marketTopic;
    }

    @Override
    public void update(MarketTopic marketTopic) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketTopicMapper mapper = sqlSession.getMapper(MarketTopicMapper.class);
        mapper.updateByPrimaryKeySelective(marketTopic);
    }
}
